import { createApp } from 'vue'

import { createPinia } from "pinia"

import router from "./router/index"
import App from './App.vue'
import piniaPluginPersistedstate from "pinia-plugin-persistedstate"

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'


import "@/assets/styles/el-reset.css"



import "@/assets/styles/common.css"


import "nprogress/nprogress.css"


import useMenu from "@/store/menulist"



import useUsers from "@/store/users"



let app = createApp(App)

app.use(ElementPlus)

let pinia = createPinia();

pinia.use(piniaPluginPersistedstate);



router.beforeEach(async (to, from) => {



    let token = sessionStorage.getItem('tk');

    if (!token) {
       

        if (to.path !== "/login") {
            return {
                path: "/login"
            }
        }

    } else {
     
        let menuStore = useMenu();



        if (menuStore.menulist.length < 1) {
      
            await menuStore.getMenuList()

            await router.replace(to.path)
        }

    }

})



router.afterEach((to, from) => {
    let usersStore = useUsers()

    if (to.meta.key) {
        usersStore.breadCrumbList = to.matched.filter(item => {
            return item.path !== "/"
        })
    }

})

app.use(router)
app.use(pinia)

app.mount('#app')
